using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using QL_Truong_Hoc.DTO;

namespace QL_Truong_Hoc.DAO
{
	public static class THocSinhAnhDAO
	{
		#region Methods

		/// <summary>
		/// Saves a record to the tHocSinhAnh table.
		/// </summary>
		public static bool Insert(Database objDb, DbTransaction objTrans, THocSinhAnhDTO tHocSinhAnh)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("tHocSinhAnh", tHocSinhAnh);

			object[] parameters = new object[]
			{
				Null.GetNull(tHocSinhAnh.HS_ID, DBNull.Value),
				Null.GetNull(tHocSinhAnh.Hs_anhdaidien, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhAnhInsert", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhAnhInsert", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tHocSinhAnhInsert", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Updates a record in the tHocSinhAnh table.
		/// </summary>
		public static bool Update(Database objDb, DbTransaction objTrans, THocSinhAnhDTO tHocSinhAnh)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("tHocSinhAnh", tHocSinhAnh);

			object[] parameters = new object[]
			{
				Null.GetNull(tHocSinhAnh.HS_ID, DBNull.Value),
				Null.GetNull(tHocSinhAnh.Hs_anhdaidien, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhAnhUpdate", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhAnhUpdate", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tHocSinhAnhUpdate", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the tHocSinhAnh table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, int hS_ID)
		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{
Null.GetNull(hS_ID, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhAnhDelete", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhAnhDelete", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tHocSinhAnhDelete", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the tHocSinhAnh table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, string strWhere)		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{strWhere
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhAnhDeleteWhere", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhAnhDeleteWhere", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tHocSinhAnhDeleteWhere", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Selects a single record from the tHocSinhAnh table.
		/// </summary>
		public static THocSinhAnhDTO Select(Database objDb, int hS_ID)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(hS_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhAnhSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects a single record from the tHocSinhAnh table.
		/// </summary>
		public static DataTable Select(Database objDb, int hS_ID, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(hS_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhAnhSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Get next index from the tHocSinhAnh table.
		/// </summary>
		public static int SelectNextIndex(Database objDb)		{
int intResult = -1;
try{
IDataReader dr = objDb.ExecuteReader("tHocSinhAnhSelectNextIndex");
if (dr != null)
{
DataTable dt = Untils.ConvertDataReaderToDataTable(dr, true);
intResult = clsCommon.fncCnvNullToInt(dt.Rows[0][0]);
}
}catch { }
return intResult;
		}

		/// <summary>
		/// Selects all records from the tHocSinhAnh table.
		/// </summary>
		public static List<THocSinhAnhDTO> SelectAll(Database objDb)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhAnhSelectAll"))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tHocSinhAnh table.
		/// </summary>
		public static List<THocSinhAnhDTO> SelectWhere(Database objDb, string strWhere)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhAnhSelectWhere", parameters))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tHocSinhAnh table.
		/// </summary>
		public static DataTable SelectAll(Database objDb, bool blnReturnDatatable = true)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhAnhSelectAll"))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tHocSinhAnh table.
		/// </summary>
		public static DataTable SelectWhere(Database objDb, string strWhere, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhAnhSelectWhere", parameters))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Creates a new instance of the THocSinhAnhDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static THocSinhAnhDTO MapDataReader(IDataReader dataReader)
		{
			THocSinhAnhDTO tHocSinhAnhDTO = new THocSinhAnhDTO();
DataTable dt = new DataTable();
dt.Load(dataReader);
if (dt.Rows.Count <= 0) return tHocSinhAnhDTO;
			tHocSinhAnhDTO.HS_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["HS_ID"]);
			tHocSinhAnhDTO.Hs_anhdaidien = clsCommon.fncCnvNullToByteArray(dt.Rows[0]["hs_anhdaidien"]);

			return tHocSinhAnhDTO;
		}
		/// <summary>
		/// Creates a new instance of the THocSinhAnhDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static List<THocSinhAnhDTO> MapDataReaderList(IDataReader dataReader)
		{
				List<THocSinhAnhDTO> lstResult = new List<THocSinhAnhDTO>();
			THocSinhAnhDTO tHocSinhAnhDTO;
DataTable dt = new DataTable();
dt.Load(dataReader);
for (int i = 0; i <= dt.Rows.Count-1; i++)
{
			tHocSinhAnhDTO = new THocSinhAnhDTO();
			tHocSinhAnhDTO.HS_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["HS_ID"]);
            tHocSinhAnhDTO.Hs_anhdaidien = clsCommon.fncCnvNullToByteArray(dt.Rows[i]["hs_anhdaidien"]);
lstResult.Add(tHocSinhAnhDTO);
}

			return lstResult;
		}

		#endregion
	}
}
